package com.hengyue.controller.system;

import com.fasterxml.jackson.databind.ObjectMapper;
//import com.hengyue.annotation.PermitAccess;
import com.hengyue.common.utils.RSAUtils;
import com.hengyue.common.utils.RedisUtils;
import com.hengyue.core.redis.RedisCache;
import com.hengyue.security.modle.LoginUser;
import com.hengyue.security.service.TokenService;
import com.hengyue.system.domain.entity.SysUser;
import com.hengyue.system.service.ISysPermissionService;
import com.hengyue.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.*;

import javax.sql.DataSource;
import java.nio.file.Path;


@RestController
@RequestMapping("/test")
@Slf4j
@RequiredArgsConstructor
public class TestController {

    private final ObjectMapper objectMapper;

    private final RedisUtils redisUtils;

    private final RedisCache redisCache;

    private final DataSource dataSource;

    private final ISysUserService sysUserService;

    private final ISysPermissionService permissionService;

    private final TokenService tokenService;



    @GetMapping("/redis")
    //@Transactional
    public Object  redisTest() throws Exception {

        SysUser sysUser = sysUserService.findUserByUserName("admin");

        LoginUser loginUser = new LoginUser(sysUser.getId(), null, sysUser, permissionService.getMenuPermission(sysUser));


        redisCache.setCacheObject("c001",loginUser);
        //
        LoginUser user = redisCache.getCacheObject("c001");

        //redisUtils.set("c001", sysUser);
        //
        //SysUser sysUser1 = redisCache.getCacheObject("c001");




        return user;
    }

    @GetMapping("/rsa")
//    @PermitAccess
    public Object test() throws Exception {
        Resource resource_1 = new ClassPathResource("rsa/publicKey.txt");
        Resource resource_2 = new ClassPathResource("rsa/privateKey.txt");
        Path path_1 = null;
        Path path_2 = null;
        String path_1_str = null;
        String path_2_str = null;
        path_1 = resource_1.getFile().toPath();
        path_1_str = resource_1.getFile().getPath();
        path_2 = resource_2.getFile().toPath();
        path_2_str = resource_2.getFile().getPath();
        //随机生成公钥和私钥
        //RSAUtils.generateKeyPair(path_1_str,path_2_str);
        System.out.println("hhh");
        String content = "我是一只小蜜蜂";
        log.info("加密内容:{}", content);
        String publicKey = RSAUtils.getPublicKey(path_1_str);
        String privateKey = RSAUtils.getPrivateKey(path_2_str);
        String encrypt = RSAUtils.encrypt(content, publicKey);
        log.info("加密后内容:{}", encrypt);
        String decrypt = RSAUtils.decrypt(encrypt, privateKey);
        log.info("解密后内容:{}", decrypt);
        return null;
    }

/*    @GetMapping("/enum1")
    public Object test1() {
        NewEntityCore entity = dao.findById(1788246735959404544L).orElse(null);
        boolean is = redisUtils.setObject("test", entity);
        log.info("DATASOURCE = " + dataSource);
        return entity;
    }

    @PostMapping("/enum")
    public void testJson(@RequestBody Criteria1 criteria) {
        System.out.print("啥情况");
    }*/

}
